package com.cat.dataStructure;

import java.util.HashSet;

/**
*   @description https://leetcode.cn/problems/maximum-number-of-non-overlapping-subarrays-with-sum-equals-target/description/
*   @author 曲大人的喵
*   @create 2025/8/19 15:04
*   @since JDK17
*/

public class Solution35 {
    public int maxNonOverlapping(int[] nums, int target) {
        int ans = 0, n = nums.length, sum = 0;
        HashSet<Integer> set = new HashSet<>();
        set.add(0);
        for (int i = 0; i < n; i++) {
            sum += nums[i];
            if (set.contains(sum - target)) {
                ans++;
                sum = 0;
                set.clear();
                set.add(0);
            } else {
                set.add(sum);
            }
        }
        return ans;
    }
}
